#include "logo_logic_handle.h"

static void print_data_hex(uint8_t *data, u16 len)
{   
    for (int i = 0; i < len; i++) 
    {
        printf(" %02x", data[i]);
    }
    printf(", len1: %d\n", len);
}


void logo_handle(SYS_OPT_KEY opt)
{
    static int time= 0;
    static u16 logo_cnt; //读取的总计数量
    static u16 cnt; //计数器
    static u8 frame_num; //帧数
    u8 buf[BUF_SIZE]; //临时存储空间
    if(app_logic_get_last_ssp() != app_logic_get_ssp()) //如果是第一上电
    {
        LOG_INFO("logo_handle\n");
        
        disp_clear_light1(); //清除原有灯效
        disp_clear_light2(); //清楚原有的灯效
        disp_clear_cw_light(); //清除cw原有灯效
        
        if (read_partition(LOGO_DATA+CNT_OFFEST,&logo_cnt,sizeof(u16))==ESP_OK)
        {
            LOG_INFO("read logo cnt is %d\n",logo_cnt);

            if (read_partition(LOGO_DATA+FRAME_NUM,&frame_num,sizeof(u8))==ESP_OK)
            {
                LOG_INFO("read logo frame_num is %d\n",frame_num);
            }
            else
            {
                LOG_ERROR("FRAME_NUM read err\n");
            }
        }
        else
        {
            LOG_ERROR("LOGO_CNT read err\n");
        }
        app_logic_set_last_ssp(app_logic_get_ssp()); //将上次的状态与本次的状态同步
        time = arch_os_ms_now();
    }
    
    
    if (arch_os_ms_elapsed(time)>= (1000/frame_num)) //按帧数读取数据
    {
        time = arch_os_ms_now();
        bzero(buf,BUF_SIZE);
        /* //用于测试灯带
        for (int i = 0; i < get_light_length()*3; i=i+3)
        {
            buf[i]=0x00;
            buf[i+1]=0x00;
            buf[i+2]=0xff;
        }
        */

        if (read_partition(LOGO_DATA+DATA_ADDR+cnt*get_color_length(),buf,get_color_length())==ESP_OK)
        {   
            //printf("cnt %d\n",cnt);
            //print_data_hex(buf,get_color_length());
            disp_light_handle_other(buf);
        }    
        cnt++;
    }
    
    if(cnt >= logo_cnt) //如果计数器满 进入关机模式
    {
        app_logic_set_ssp(SSP_POWER_OFF); //进入关机模式
        //task_wifi_init();
        cnt=0; //计数器清零
    }
    
}